package com.hdb.processor;


import org.apache.commons.lang3.StringUtils;

import com.hdb.pojo.News;
import com.hdb.pojo.NewsCss;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

public class ProcessorTest implements PageProcessor {
	private NewsCss newsCss;
	private static int count = 0;;

	public ProcessorTest() {

	}

	public static int getCount() {
		return count;
	}

	public static void setCount(int count) {
		ProcessorTest.count = count;
	}

	public ProcessorTest(NewsCss newsCss) {
		this.newsCss = newsCss;

	}

	// 配置
	private Site site = Site.me().setRetryTimes(3).setSleepTime(2000).setTimeOut(5000);

	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}

	public void process(Page page) {
		// 新建一个新闻对象
		News news = new News();
		String resource = newsCss.getResource();
		// 获取分类新闻
		
		/*if (newsCss.getListCss()!=null&&page.getUrl().regex(resource).match()) {
			
			List<String> all = page.getHtml().xpath(newsCss.getListCss()).all();
			
			if (!all.toString().contains("http")) {
				List<String> url = UrlStringUtils.addURL(all, resource);
				
				page.addTargetRequests(url);
			} else {
				page.addTargetRequests(all);
			}
		}*/

		Boolean flag = page.getUrl().regex(newsCss.getUrl()).match();
		if (!flag) {
			// 获取所有满足条件的url
			// https://www.theguardian.com/[0-9
			// a-z]+/[0-9]{4}/[a-z]+/[0-9]{2}/[A-Z 0-9 -]+
			// https://www.theguardian.com/[0-9
			// a-z]+/[0-9]{4}/[a-z]+/[0-9]{2}/[A-Z 0-9 -]+
			// System.out.println(newsCss.getUrl());
			page.addTargetRequests(page.getHtml().links().regex(newsCss.getUrl()).all());

		}
		count++;
		// 获取url
		String Url = page.getUrl().get();
		news.setUrl(Url);
		// 获取标题..
		String title = page.getHtml().$(newsCss.getTitle(), "allText").all().toString();
		//System.out.println(title);
		if (StringUtils.isNotEmpty(title)) {
			page.setSkip(true);

		}
		news.setTitle(title);
		// 获取关键字
		// String articalKeyWords=page.getHtml().$("div.submeta__section-labels
		// news.setArticalKeyWords(articalKeyWords);
		// 获取日期.
		if (StringUtils.isNotEmpty(newsCss.getGetTime())) {
			String GetTime = page.getHtml().$(newsCss.getGetTime(), "allText").all().toString();
			news.setGetTime(GetTime);
		}
		// 获取正文.
		if (StringUtils.isNotEmpty(newsCss.getTxt())) {
			String Txt = page.getHtml().$(newsCss.getTxt(), "allText").all().toString();
			news.setTxt(Txt);
		}

		// 获取摘要 .
		if (StringUtils.isNotEmpty(newsCss.getSummary())) {
			String Summary = page.getHtml().$(newsCss.getSummary(), "allText").all().toString();
			news.setSummary(Summary);
		}
		// 获取文章类型.
		if (StringUtils.isNotEmpty(newsCss.getTxtType())) {
			String TxtType = page.getHtml().$(newsCss.getTxtType(), "allText").all().toString();
			news.setTxtType(TxtType);
		}

		// 获取作者.
		if (StringUtils.isNotEmpty(newsCss.getAuthor())) {
			String author = page.getHtml().$(newsCss.getAuthor(), "allText").all().toString();
			news.setAuthor(author);
		}

		// 获取来源
		if (StringUtils.isNotEmpty(newsCss.getSource())) {
			String Source = page.getHtml().$(newsCss.getSource(), "allText").all().toString();
			news.setSource(Source);
		}

		page.putField("title", news.getTitle());
		page.putField("TitleCh", news.getTitleCh());
		page.putField("Title_Score", news.getTitle_Score());
		page.putField("articalKeyWords", news.getArticalKeyWords());
		page.putField("Txt", news.getTxt());
		page.putField("TxtCh", news.getTxtCh());
		page.putField("Txt_Score", news.getTxt_Score());
		page.putField("Url", news.getUrl());
		page.putField("Summary", news.getSummary());
		page.putField("SummaryCh", news.getSummaryCh());
		page.putField("TxtType", news.getTxtType());
		page.putField("GetTime", news.getGetTime());
		page.putField("opicType", news.getOpicType());
		page.putField("Source", news.getSource());
		page.putField("isdown", news.getIsdown());
		page.putField("relateNewsNumber", news.getRelateNewsNumber());
		page.putField("KeyWords", news.getKeyWords());
		page.putField("schoolName", news.getSchoolName());
		page.putField("country", news.getCountry());
		page.putField("Polar", news.getPolar());
		page.putField("author", news.getAuthor());
		page.putField("language", news.getLanguage());
		page.putField("htmlCode", news.getHtmlCode());
		page.putField("semanticValue", news.getSemanticValue());
		page.putField("mediaIntroduction", news.getMediaIntroduction());
		page.putField("zuanfaNum", news.getZuanfaNum());
		page.putField("pinlunNum", news.getPinlunNum());
		page.putField("shouchangNum", news.getSchoolName());
		page.putField("txtSeg", news.getTxtSeg());
	}

}
